package org.dey.controller;

import org.dey.entity.Result;
import org.dey.pojo.Menu;
import org.dey.pojo.PageVo;
import org.dey.service.MenuService;
import org.dey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class MenuController {

    @Autowired
    MenuService menuService;

    @Autowired
    UserService userService;

    //左侧菜单栏数据
    @GetMapping("zcmenus.action/{userid}")

    public List<Menu> zcmenus(@PathVariable int userid) {

        List<Menu> zcmenus = menuService.zcmenus(userid);

        return zcmenus;

    }

    @PostMapping(value = "menu.action/{pagecurrent}/{pagesize}")
    public PageVo<Menu> menulist(@RequestBody Menu menu, @PathVariable int pagecurrent, @PathVariable int pagesize) {

        PageVo<Menu> allmenu = menuService.querymenu(menu, pagecurrent, pagesize);

        return allmenu;

    }
        //添加
        @PostMapping("menu.action")
        public Result addmenu (@RequestBody Menu menu){

            Result insertmenu = menuService.insertmenu(menu);

            return insertmenu;
        }

        //删除
        @DeleteMapping("menu.action/{id}")
        public Result delmenubyid ( @PathVariable Integer id){

            Result delmenu = menuService.delmenu(id);

            return delmenu;
        }
    //修改先根据id查到该数据
    @GetMapping("menu.action/{id}")
    public Menu querymenubyid(@PathVariable Integer id){

        Menu querymenubyid = menuService.querymenubyid(id);

        return querymenubyid;
    }

    //根据id修改
    @PutMapping("menu.action")
    public Result updatemenubyid(@RequestBody Menu menu){

        Result updatemenubyid = menuService.updatemenubyid(menu);

        return updatemenubyid;
    }
    //获取全部信息 在授权页面展示
    @GetMapping("menusQbXx.action")
    public List<Menu> menusQbXx(){

        List<Menu> list = menuService.menusQbXx();

        return list;
    }

    //查询角色已有的菜单
    @GetMapping("menusBfXx.action/{rid}")
    public int[] menusBfXx(@PathVariable int rid){

        int[] ints = menuService.menusBfXx(rid);

        return ints;
    }

    //授权
    @PostMapping("shouquan.do/{roleid}")
    public Result shouquan(@PathVariable int roleid,@RequestBody int[] checkedKeys){

       Result result =  menuService.shouquan(roleid,checkedKeys);

        return result;
    }
    }
